//
//  ZTViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/7.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  点击自提调的接口


#import "ZTViewController.h"
#import "ZTTableViewCell.h"
#import "ZTModel.h"
@interface ZTFuJinDeCanTinHeaderView:UIView
/**附近的餐厅label*/
@property (nonatomic, strong) UILabel *fujindeCanTinLable;
/**红色的线*/
@property (nonatomic, strong) UIView *redLineView;
/**灰色的线*/
@property (nonatomic, strong) UIView *lightGrayLineView;

@end

@implementation ZTFuJinDeCanTinHeaderView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)setupUI {
    [self addSubview:self.fujindeCanTinLable];
    [self addSubview:self.redLineView];
    [self addSubview:self.lightGrayLineView];
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.fujindeCanTinLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.height.mas_equalTo(22);
        make.width.mas_greaterThanOrEqualTo(20); //自适应宽度
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.redLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.fujindeCanTinLable);
        make.height.mas_equalTo(3);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
    [self.lightGrayLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(self.mas_bottom);
    }];
}

#pragma makr - getter
- (UILabel *)fujindeCanTinLable {
    if (!_fujindeCanTinLable) {
        _fujindeCanTinLable = [UILabel new];
        _fujindeCanTinLable.text = NSLocalizedString(L_FuJinDeCanTing, @"附近的餐厅");
    }
    return _fujindeCanTinLable;
}
- (UIView *)redLineView {
    if (!_redLineView) {
        _redLineView = [UIView new];
        _redLineView.backgroundColor = kMainRedColor;
    }
    return _redLineView;
}
- (UIView *)lightGrayLineView {
    if (!_lightGrayLineView) {
        _lightGrayLineView = [UIView new];
        _lightGrayLineView.backgroundColor = kMainLightGrayColor;
    }
    return _lightGrayLineView;
}


@end





@interface ZTViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    ZTModel *_selectedModel; /**记录选中的model*/
}

@property(nonatomic, strong) UITableView *tv;

@property (nonatomic, strong) NSMutableArray *dataArrM;
/**确定*/
@property (nonatomic, strong) UIButton *sureButton;
/**区头*/
@property (nonatomic, strong) ZTFuJinDeCanTinHeaderView *headerView;

@end

@implementation ZTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = NSLocalizedString(L_XuanZheCanTing,@"选择餐厅");
    
    [self loadData];
    
    [self setupView];
}

- (void)setupView {
    self.tv.frame = self.view.bounds;
    CGFloat y = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin - 40;
    self.sureButton.frame = CGRectMake(0, y, SCREEN_WIDTH, 40);
    
    [self.view addSubview:self.tv];
    self.tv.tableHeaderView = self.headerView;
    self.tv.tableFooterView = [UIView new];
    
    [self.view addSubview:self.sureButton];
}


- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tv.delegate = self;
        _tv.dataSource = self;
        //注册cell
        [_tv registerNib:[UINib nibWithNibName:@"ZTTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZTTableViewCell"];
        
        //取消cell样式
        _tv.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tv;
}
- (NSMutableArray *)dataArrM {
    if (!_dataArrM) {
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

- (ZTFuJinDeCanTinHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZTFuJinDeCanTinHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    }
    return _headerView;
}

- (UIButton *)sureButton {
    if (!_sureButton) {
        //国际化
        NSString *sureStr = NSLocalizedString(Sure, @"确认");
        _sureButton = [UIButton createFastRedBtnRadius:0 target:self action:@selector(clickSureBtnAction:) title:sureStr];
    }
    return _sureButton;
}
- (void)clickSureBtnAction:(UIButton *)btn {
 
    if (!_selectedModel) {
        [JRToast showWithText:NSLocalizedString(ToastPleaseCanTing, @"请选择餐厅")];
        return;
    }
    
    if (self.ztVCBlock) {
        self.ztVCBlock(_selectedModel);
    }
    
    //回调模型
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZTTableViewCell"];
    
    cell.model = self.dataArrM[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //排他思想
    for (ZTModel *model in self.dataArrM) {
        model.isSelected = NO;
    }
    
    //当前标记为YES
    ZTModel *selectedModel = self.dataArrM[indexPath.row];
    selectedModel.isSelected = YES;
    _selectedModel = selectedModel; //存一份
    
    //刷新
    [self.tv reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60; //50;
}


#pragma mark - loaddata
- (void)loadData {
    [FDHttpAPi requestSYShopListSuccessBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
    
        if ([event isEqualToString:@"SUCCESS"]) {
            
            NSArray *data = responseObjc[@"data"];
            if(data.count == 0) return;
            
            for (NSDictionary *dic in data) {
                ZTModel *model = [ZTModel mj_objectWithKeyValues:dic];
                
//这个界面，需判断门店信息字段reservation是否=1，如果=1，则显示这个门店，=0则不显示
                if ([model.selfxx isEqualToNumber:@(1)]) { //显示
                    [self.dataArrM addObject:model];
                }
            }
            
            //刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv reloadData];
            });
            
            [self.tv.mj_header endRefreshing]; //结束刷新
        }else {
            [JRToast showWithText:responseObjc[@"describe"]];
        }
    } failureBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}
@end
